var CreateDpyxController = null;
(function(doc, win) {
	
 
    
    // sui 页面就绪时间
    $(doc).on("pageInit", "#createDpyxActivityPage", function(e, pageId, $page,isBack) {
    	if(isBack){
    		var paramObj = CommonUtil.takeOutParamObject(); 
        	if(paramObj.createActivityParams!=undefined){
        		createActivityPublicParam = paramObj.createActivityParams;
        	}
			return;
		}
        createDpyxController = new CreateDpyxController();
		ActivityPreloadData.loadDataTimer({
					"func0" : function() {
					    ActivityPreloadData.createPrerequisite();					 			
					},
					"callBackEvent" : function() {
						createDpyxController.initPageData();
					}
	     }); 	
    	  	
    });
  
    
    /**
     * 下一步;
     */
    $(doc).on("click", "#createDpyxActivityPage #nextBtn", function() {      
    	if(createDpyxController.checkVoucherData()==true){
            createDpyxController.saveVoucherData();        
        	var paramObj = CommonUtil.takeOutParamObject();     	 
            if(paramObj.isFristRoute==undefined){
        		 CommonUtil.injectParamObject({"activityType":ACTIVITY_TYPE.ITEM_MARKET.val,"reload":true,"createActivityParams":createActivityPublicParam});
        		 $.router.load("create_activity_xhyzm.html",true);
        	}else{
        		 CommonUtil.injectParamObject({"activityType":ACTIVITY_TYPE.ITEM_MARKET.val,"reload":false,"createActivityParams":createActivityPublicParam});
        		 $.router.load("create_activity_xhyzm.html");
        	}
        
       	   
    	}
    	 
    });
    
     
    /**
     * 商品图片上传事件;
     */
    $(doc).on("click","#createDpyxActivityPage #goodsPic",function(){
    	$.actions([[{
			"text" : "相册",
			"onClick" : function(){
				 AppUtil.call(AppUtil.APIS.SELECT_PICTURE,{"compress":{"sizeLimit":"500","unit":"K"},"crop":{"heightRatio":"1","widthRatio":"1"},"format":"JPG","maxNum":"1"},
			                function(selectResult){
			                	if(selectResult.success.toString() == "true"){
			                		if(selectResult.resultObject && selectResult.resultObject instanceof Array){
			                		    createActivityPublicParam.goodsInfo.localGoodsPicPath = selectResult.resultObject[0];   		    
			                			common.loading("图片上传中...");
			                			AppUtil.call(AppUtil.APIS.UPLOAD_PICTURE,
			                	                {
			                				       "file":createActivityPublicParam.goodsInfo.localGoodsPicPath,
			                				       "url":App.serverUrl + "app/img/uploadActivityImg.do"
			                	                },
			                	                function(uploadResult){
			                	                	if(uploadResult.success.toString() == "true" && uploadResult.resultObject.success.toString() == "true"){
			                	                		if(uploadResult.resultObject && uploadResult.resultObject != null){
			                	                			createActivityPublicParam.goodsInfo.goodsPicUrl = App.previewUrlPrefix + uploadResult.resultObject.resultObject.imageUrl;
			                	                			createActivityPublicParam.goodsInfo.goodsPicId = uploadResult.resultObject.resultObject.imageId;  
			                	                			createDpyxController.initDpyxPicData();
			                	                		}else{
			                	                			DialogUtil.dialogWarn("未正确返回图片上传结果!");            			
			                	                		}
			                	                	         		
			                	                	}else if(result.code == -1){
			                	                		  DialogUtil.dialogWarn(uploadResult.msg);         		 
			                	                	} 
			                	                	common.closeLoading();
			                	            });
			                			
			                		
			                		}
			                	         		
			                	}else{
			                		  DialogUtil.dialogWarn("选择图片异常!"+selectResult.msg);
			                		  return;
			                	}                 
			            });
			}
		},{
			"text" : "拍照",
			"onClick" : function(){
				 AppUtil.call(AppUtil.APIS.TAKE_PICTURE,{"compress":{"sizeLimit":"500","unit":"K"},"crop":{"heightRatio":"1","widthRatio":"1"},"format":"JPG","maxNum":"1"},
			                function(selectResult){
			                	if(selectResult.success.toString() == "true"){
			                		if(selectResult.resultObject && selectResult.resultObject!=""){
			                		    createActivityPublicParam.goodsInfo.localGoodsPicPath = selectResult.resultObject;   		    
			                			common.loading("图片上传中...");
			                			AppUtil.call(AppUtil.APIS.UPLOAD_PICTURE,
			                	                {
			                				       "file":createActivityPublicParam.goodsInfo.localGoodsPicPath,
			                				       "url":App.serverUrl + "app/img/uploadActivityImg.do"
			                	                },
			                	                function(uploadResult){
			                	                	if(uploadResult.success.toString() == "true" && uploadResult.resultObject.success.toString() == "true"){
			                	                		if(uploadResult.resultObject && uploadResult.resultObject != null){
			                	                			createActivityPublicParam.goodsInfo.goodsPicUrl = App.previewUrlPrefix + uploadResult.resultObject.resultObject.imageUrl;
			                	                			createActivityPublicParam.goodsInfo.goodsPicId = uploadResult.resultObject.resultObject.imageId;  
			                	                			createDpyxController.initDpyxPicData();
			                	                		}else{
			                	                			DialogUtil.dialogWarn("未正确返回图片上传结果!");            			
			                	                		}
			                	                	         		
			                	                	}else if(result.code == -1){
			                	                		  DialogUtil.dialogWarn(uploadResult.msg);         		 
			                	                	} 
			                	                	common.closeLoading();
			                	            });
			                			
			                		
			                		}
			                	         		
			                	}else{
			                		  DialogUtil.dialogWarn("选择图片异常!"+selectResult.msg);
			                		  return;
			                	}                 
			            });
			}			
		}],[{
			"text" : "取消",
			"color" : "danger",
			"onClick" : function(){
			}
		}]]);
     	
    });
    
    
    
    
    
    /**
     * 返回事件;
     */
    $(doc).on("click","#createDpyxActivityPage .back",function(){
    	ActivityPreloadData.clearCreateActivityParam(); 
    });
      
    
    /**
     * 选择代金券有效日期类型;
     */
    $(doc).on("click", "#createDpyxActivityPage #quan-deadline.switch-label", function() {
    	if($("#createDpyxActivityPage .actions-modal-button").size()>0){
			//应该关闭模态窗口(具体怎么关由伟胜提供方法)
			$.closeModal($("#createDpyxActivityPage .actions-modal"));
		}
    	 var _text = $(this).children("span");
         var _self = $(this);
         var buttons1 = [
             {
                 text: '相对时间',
                 onClick: function () {
                     _text.text("相对时间");
                     _self.siblings("p").hide().eq(0).show();
                 }
             },
             {
                 text: '指定时间',
                 onClick: function () {
                	     if(createActivityPublicParam.autoDelayFlag == "Y"){
                	    		common.confirm({
    								"title":"",
    								"content": "选择指定时间，将会关闭自动续期功能，确定要选择指定时间吗?",
    								"btnRight": "确定",
    								"btnLeft": "取消",
    								 callbackLeft: function () {//取消
    									return false;
    								},
    								 callbackRight: function () {//确定
    									_text.text("指定时间");
    									_self.siblings("p").hide().eq(1).show();
    									createActivityPublicParam.autoDelayFlag = 'N';//关闭自动续期;		
    									return false;
    								}
    				           }); 
                	    	 
                	     }else{
                	    	 _text.text("指定时间");
						     _self.siblings("p").hide().eq(1).show(); 
                	     }
						           	
                	
                 }
             }
         ];
         var buttons2 = [
             {
                 text: '取消',
             }
         ];
         var groups = [buttons1, buttons2];
         $.actions(groups);
    });
    
   
    $(doc).on('input', '#createDpyxActivityPage #voucherAmount,#createDpyxActivityPage #relativeValidDays,#createDpyxActivityPage #voucherMinConsumeAmount', function (e) {
    	var reg = new RegExp("^[0-9]+\.{0,1}[0-9]{0,2}$"); 	 
    	var inputVal = $(this).val();
    	var inputId = $(this).attr("id");
    	if(inputVal == ""){
    		$(this).val("");
    	}else{
    		if(!reg.test(inputVal)){
        		var indexOfPoint =inputVal.indexOf(".");
            	if(indexOfPoint != -1){
            		if(inputVal.substring(indexOfPoint+1).length>2){
            			$(this).val(inputVal.substring(0,indexOfPoint+3));
            		}
            	} 
        	}else{//通过正则过滤;
        		if(inputVal.charAt(0)=='0'&& inputVal.charAt(1)!='' && inputVal.charAt(1)!='.'){
        			$(this).val(Number(inputVal));
        		}else if(inputId == "voucherAmount" && Number(inputVal) < 1){//代金券面额只能大于等于1;
        			$(this).val(1);
        		}
        	}
          	
    	}
	});
 
    
})(document, window);